package com.charactereducation.serviceproviderusercenter.service;

import com.charactereducation.dao.ApiConfigMapper;
import com.charactereducation.module.vo.ApiCofig;
import com.charactereducation.tools.MD5Tools;
import com.charactereducation.tools.myRunTime.MyRunTimeExcption;
import com.charactereducation.tools.redis.RedisUtil;
import com.charactereducation.tools.result.ReturnCode;
import com.charactereducation.webcore.service.ServiceBase;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Baggio on 2019-2-18.
 */
@Service
public class ApiCofigServiceImpl extends ServiceBase implements ApiCofigService {
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    ApiConfigMapper apiConfigMapper;

    @Override
    public void modifyApiCofig(ApiCofig apiCofig) throws MyRunTimeExcption {
        if (apiCofig.getTid() == null) {
            this.insertApiConfig(apiCofig);
            return;
        }
        this.updateApiConfig(apiCofig);
    }

    @Override
    public ApiCofig getApiCofig(Map<String, String> map) throws MyRunTimeExcption {
        return apiConfigMapper.getApiCofig(map);
    }

    @Override
    public List<ApiCofig> getApiCofigList(Map<String, String> map) throws MyRunTimeExcption {
        return apiConfigMapper.getApiCofigList(map);
    }

    @Override
    public void checkAPI(String channelId, String timestamp, String sign) throws MyRunTimeExcption {
        if (StringUtils.isBlank(channelId)) {
            throw new MyRunTimeExcption(ReturnCode.CHANNELID_IS_NULL);
        }


        if (StringUtils.isBlank(timestamp)) {
            throw new MyRunTimeExcption(ReturnCode.TIMESTAMP_IS_NULL);
        }
        if (StringUtils.isBlank(sign)) {
            throw new MyRunTimeExcption(ReturnCode.SIGN_IS_NULL);
        }

        ApiCofig apiCofig = (ApiCofig) redisUtil.get(channelId);

        if (apiCofig == null) {
            apiCofig = apiConfigMapper.getApiCofig(new HashMap<String, String>() {
                {
                    put("channelId", channelId);
                    put("status", String.valueOf(ApiCofig.STATUS_VALID));
                }
            });
        }


        if (apiCofig == null) {
            throw new MyRunTimeExcption(ReturnCode.CHANNELID_IS_INEXISTENCE);
        }
        if (StringUtils.isBlank(apiCofig.getChannelSecret())) {
            throw new MyRunTimeExcption(ReturnCode.CHANNELID_IS_INEXISTENCE);
        }
        redisUtil.set(channelId, apiCofig);

//            sign SIGN=md5(channelId+channeleScret+timestamp)
        String signTemp = MD5Tools.MD5(channelId + apiCofig.getChannelSecret() + timestamp);
        logger.info(signTemp);
        if (!signTemp.equals(sign)) {
            throw new MyRunTimeExcption(ReturnCode.SIGN_IS_INVALID);
        }
    }


    public void insertApiConfig(ApiCofig apiCofig) throws MyRunTimeExcption {
        apiConfigMapper.insertApiCofig(apiCofig);
    }

    public void updateApiConfig(ApiCofig apiCofig) throws MyRunTimeExcption {
        apiConfigMapper.updateApiCofig(apiCofig);
    }
}
